package mars.io.formatters

import mars.ns.core.Network
import mars.ns.core.Node
import mars.ns.HasState

import java.io.ByteArrayOutputStream
import java.io.ObjectOutputStream
import java.io.ObjectInputStream
import java.io.ByteArrayInputStream

class SerializedFormatter[T] extends Formatter[T] {
    
	def format(obj: T): String = {
	    val baos: ByteArrayOutputStream = new ByteArrayOutputStream()
	    val oos: ObjectOutputStream = new ObjectOutputStream(baos)
	    oos.writeObject(obj)
	    return new sun.misc.BASE64Encoder().encodeBuffer(baos.toByteArray())
	}

    def fromString(str: String): T = {
	    val bytes = new sun.misc.BASE64Decoder().decodeBuffer(str)
        val bais: ByteArrayInputStream = new ByteArrayInputStream(bytes)
        val ois: ObjectInputStream = new ObjectInputStream(bais)
        return ois.readObject().asInstanceOf[T]
    }

    def validFormat(str: String): Boolean = true
}